//
//  CreateActiveDetailsViewController.m
//  Findif
//
//  Created by Mac on 2019/3/6.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "CreateActiveDetailsViewController.h"
#import "CreateActiveHeaderView.h"
#import "CreateGroupActiveTableViewCell.h"
#import "GrayFooterView.h"
#import "ActiveInfoTableViewCell.h"
#import "GroupActiveViewController.h"

@interface CreateActiveDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * array;
@property (nonatomic, strong)NSIndexPath * indexPath;
@property (nonatomic, strong)ActiveInfoTableViewCell *cell;

@end

@implementation CreateActiveDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.array = @[@"活动项目",@"活动时间",@"活动地址"];
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"活动";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"发布" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didSetUpButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF0F0F0);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF0F0F0);
//    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[CreateGroupActiveTableViewCell class] forCellReuseIdentifier:@"CreateGroupActiveTableViewCell"];
    [self.tableView registerClass:[CreateActiveHeaderView class] forHeaderFooterViewReuseIdentifier:@"CreateActiveHeaderView"];
    [self.tableView registerClass:[ActiveInfoTableViewCell class] forCellReuseIdentifier:@"ActiveInfoTableViewCell"];
    [self.tableView registerClass:[GrayFooterView class] forHeaderFooterViewReuseIdentifier:@"CreateActiveGrayFooterView"];
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self loadData];
    //    }];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 4;
    }else{
        
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
       
        CreateGroupActiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CreateGroupActiveTableViewCell" forIndexPath:indexPath];
        
        
        if (indexPath.row == 0) {
            
            self.indexPath = indexPath;
            cell.titleLabel.hidden = YES;
            cell.subLabel.hidden = YES;
            cell.textFiled.hidden = NO;
            cell.joinButton.hidden = YES;
            
        }else{
            
            cell.textFiled.hidden = YES;
            cell.joinButton.hidden = YES;
            cell.titleLabel.hidden = NO;
            cell.subLabel.hidden = NO;
            
            cell.titleLabel.text = self.array[indexPath.row - 1];
            if (indexPath.row == 1) {
                
                cell.subLabel.text = self.dict[@"merchants_goods_name"];
            }else if (indexPath.row == 2){
                
                cell.subLabel.text = self.dateString;
            }else if (indexPath.row == 3){
                
                cell.subLabel.text = self.dict[@"merchants_address"];
            }
        }
        
        return cell;
    }else{
        
        self.cell = [tableView dequeueReusableCellWithIdentifier:@"ActiveInfoTableViewCell" forIndexPath:indexPath];
        
        return self.cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 200 * SCREENH6;
    }else{
        
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 10 * SCREENH6;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        CreateActiveHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CreateActiveHeaderView"];
        
        header.dict = self.dict;
        
        return header;
    }else{
        
        return nil;
    }
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    GrayFooterView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CreateActiveGrayFooterView"];
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 50 * SCREENH6;
    }else{
        
        return 180 * SCREENH6;
    }
}


- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{
    
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF0F0F0);
    }
}

- (void)didSetUpButton{
    
    CreateGroupActiveTableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
    
    if (cell.textFiled.text.length >= 4) {
        
        NSString * goods_id = [NSString stringWithFormat:@"%@",self.dict[@"id"]];
        NSString * info;
        if ([self.cell.textView.text isEqualToString:@"填写活动介绍，让大家更了解你的活动"]) {
            
        }else{
            
            info = self.cell.textView.text;
        }
        
        NSDictionary * dict = @{@"group_id":self.group_id,@"goods_id":goods_id,@"activity_title":cell.textFiled.text,@"activity_time":self.dateString,@"activity_info":info};
        
        [[ZRNetworkTool sharedTool] addGroupActivityGoodsWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          for (UIViewController *controller in self.navigationController.viewControllers) {
                                                                              if ([controller isKindOfClass:[GroupActiveViewController class]]) {
                                                                                  GroupActiveViewController *A =(GroupActiveViewController *)controller;
                                                                                  [self.navigationController popToViewController:A animated:YES];
                                                                              }
                                                                          }
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
        
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"活动名称至少4个字"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
